package com.xinziruo.richclient.engine.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;

import com.xinziruo.richclient.engine.CrawlResponse;

/**
 * 
 * @author loudyn
 * 
 */
public class HttpClientCrawlResponse implements CrawlResponse {

	private final HttpResponse response;
	private long elapsedTime;
	private InputStream content;

	public HttpClientCrawlResponse(HttpResponse response, long elapsedTime) throws Exception {

		this.response = response;
		this.elapsedTime = elapsedTime;

		copyContent(response);
	}

	private void copyContent(HttpResponse response) throws IllegalStateException, IOException {
		HttpEntity entity = response.getEntity();

		try {

			content = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
		} finally {
			// we must release the entity.
			EntityUtils.consume(entity);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.crawl.domain.intruder.IntruderResponse#getStatusCode()
	 */
	@Override
	public int getStatusCode() {
		StatusLine status = response.getStatusLine();
		if (null != status) {
			return status.getStatusCode();
		}

		throw new IllegalStateException("can't get statusCode!");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.youboy.crawl.domain.intruder.IntruderResponse#getContentCharset()
	 */
	@Override
	public String getContentCharset() {
		HttpEntity entity = response.getEntity();
		return EntityUtils.getContentCharSet(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.crawl.domain.intruder.IntruderResponse#getContentLength()
	 */
	@Override
	public long getContentLength() {
		HttpEntity entity = response.getEntity();
		return entity.getContentLength();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.crawl.domain.intruder.IntruderResponse#getElapsedTime()
	 */
	@Override
	public long getElapsedTime() {
		return elapsedTime;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.crawl.domain.intruder.IntruderResponse#getMimetype()
	 */
	@Override
	public String getMimetype() {
		HttpEntity entity = response.getEntity();
		return EntityUtils.getContentMimeType(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.crawl.domain.intruder.IntruderResponse#getLocation()
	 */
	@Override
	public String getLocation() {
		Header header = response.getFirstHeader("Location");
		if (null != header) {
			return header.getValue();
		}

		return null;
	}

	@Override
	public InputStream getContent() throws IOException {
		return content;
	}

	public String getCookies() {
		Header header = response.getFirstHeader("Cookie");
		if (null != header) {
			return header.getValue();
		}

		return null;
	}

	@Override
	public void consumeContent() throws IOException {
		IOUtils.closeQuietly(this.content);
	}

}
